package com.kingcore.commerce.base.controller;

import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.kingcore.commerce.base.dao.CommerceDao;
import com.kingcore.commerce.core.editor.DateEditor;
import com.kingcore.commerce.core.editor.DoubleEditor;
import com.kingcore.commerce.core.editor.IntegerEditor;
import com.kingcore.commerce.core.editor.LongEditor;

public abstract class CommerceController<T, PK extends java.io.Serializable> {

	protected static Logger log = Logger.getLogger(CommerceController.class);
	
	//---------------------------------------------------------------------->>
	//public abstract CommerceDao<T, PK> getModuleDao();
	protected abstract String getModuleParent();
	protected abstract String getModuleName();
	protected CommerceDao<T, PK> getModuleDao(){
		log.error("请先实现本方法!");
		return null;
	} 
	
	public abstract String neww(HttpServletResponse response, Model model) throws Exception;
	
	public abstract String edit(HttpServletResponse response, Model model, PK pk) throws Exception;
	
	public abstract String list(HttpServletResponse response, Model model, T t, Integer pageNum, Integer numPerPage) throws Exception;
	
	public abstract ModelAndView add(HttpServletResponse response, Model model, T t) throws Exception;
	
	public abstract ModelAndView mod(HttpServletResponse response, Model model, T t) throws Exception;
	
	public abstract ModelAndView remove(HttpServletResponse response, Model model, PK pk) throws Exception;
	
	//----------------------------------------------------------------------<<

//	protected Pagination getNavigator(Integer pageNum, Integer numPerPage,
//			Integer totalCount) {		
//		
//		if(log.isDebugEnabled()){
//			log.debug("pageNum="+pageNum
//					+ ",numPerPage="+numPerPage
//					+ ",totalCount="+totalCount);
//		}
//		
//		return new WebPageNavigator(pageNum,numPerPage,totalCount);
//	}
 
//	protected void setNavigator2Model(Model model, Pagination pagination) {
//		model.addAttribute("pagination", pagination );
//		
//		//以下是为了兼容
//		model.addAttribute("totalCount", pagination.getRowCount());
//		model.addAttribute("numPerPage", pagination.getPageSize());
//		model.addAttribute("pageNum",   pagination.getPageNumber());
//		
//		System.out.println("currentPage="+ pagination.getPageNumber() );
//		
//	}
	
//	public abstract String neww(Model model) throws Exception;
//	public abstract String edit(Model model, PK pk) throws Exception;
//	public abstract String list(Model model, WebPageNavigator pagination, T t) throws Exception;  //Pagination
//	public abstract ModelAndView add(T t) throws Exception;	
//	public abstract ModelAndView remove(PK pk) throws Exception; 
//	public abstract ModelAndView mod(T t) throws Exception;
	
	//@Autowired
	//protected CommerceDao<T, PK> dao;
	
	
	@Autowired
	protected ResourceBundleMessageSource _res;
	
//	@Autowired
//	protected SpringContextHolder _contextHolder;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		
		binder.registerCustomEditor(int.class, new IntegerEditor());
		//binder.registerCustomEditor(Integer.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Date.class, new DateEditor());
	}
	
	protected ModelAndView ajaxDone(int statusCode, String message) {
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", statusCode);
		mav.addObject("message", message);
		return mav;
	}

	protected ModelAndView ajaxDoneSuccess(String message) {
		return ajaxDone(200, message);
	}

	protected ModelAndView ajaxDoneError(String message) {
		return ajaxDone(200, message);
	}
	protected String getMessage(String code) {
		return this.getMessage(code, new Object[] {});
	}

	protected String getMessage(String code, Object arg0) {
		return getMessage(code, new Object[] { arg0 });
	}

	protected String getMessage(String code, Object arg0, Object arg1) {
		return getMessage(code, new Object[] { arg0, arg1 });
	}

	protected String getMessage(String code, Object arg0, Object arg1,
			Object arg2) {
		return getMessage(code, new Object[] { arg0, arg1, arg2 });
	}

	protected String getMessage(String code, Object arg0, Object arg1,
			Object arg2, Object arg3) {
		return getMessage(code, new Object[] { arg0, arg1, arg2, arg3 });
	}

	protected String getMessage(String code, Object[] args) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
		Locale locale = localeResolver.resolveLocale(request);

		return _res.getMessage(code, args, locale);
	}
	

	
	protected HttpServletRequest getRequest(){
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
		HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest(); 
		return request;
	}
	
}
